package com.xxs.model;

import java.util.List;

import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.ehcache.CacheKit;
import com.xxs.common.Const;
import com.xxs.ext.jfinal.Model;

/**
 */
@SuppressWarnings("serial")
public class Goods extends Model<Goods>{

    public static final Goods dao = new Goods();
    private static final String GOODS_CACHE = "goods";
    private static final String GOODS_PAGE_FOR_INDEX_CACHE = "goodsPageForIndex";
    private static final String GOODS_PAGE_FOR_MODULE_CACHE = "goodsPageForModule";
    private static final String HOT_GOODS_PAGE_CACHE = "hotGoodsPage";
    private static final String NICE_GOODS_PAGE_CACHE = "niceGoodsPage";
    private static final String GOODS_PAGE_FOR_ADMIN_CACHE = "goodsPageForAdmin";
    private static final String GOODS_PAGE_FOR_USER_CACHE = "goodsPageForUser";
    private static final String GOODS_LIST_CACHE = "goodsList";

    public Goods(){
        super(GOODS_CACHE);
    }
    
    /* get */
    public Goods get(int id){
        return loadModel(id);
    }
    public Page<Goods> getPage(int pageNumber){
        Page<Goods> goodsPage = dao.paginateByCache(GOODS_PAGE_FOR_INDEX_CACHE, pageNumber,
                pageNumber, Const.GOODS_PAGE_SIZE,
                "select id", "from goods order by isUp desc,createTime desc");
        return loadModelPage(goodsPage);
    }
    public Page<Goods> getPageForModule(int moduleID, int pageNumber){
        Page<Goods> goodsPage = dao.paginateByCache(GOODS_PAGE_FOR_MODULE_CACHE, moduleID + "-" + pageNumber,
                pageNumber, Const.GOODS_PAGE_SIZE,
                "select id", "from goods where moduleID=? order by isUp desc,createTime desc", moduleID);
        return loadModelPage(goodsPage);
    }
    
    public List<Goods> getHotTopinList(int count){
   	 return dao.findByCache(GOODS_LIST_CACHE, 1, "select * from goods order by isUp desc,pv desc limit 0,?",count);
    }
    
    public Page<Goods> getNicePage(int pageNumber){
    	String cacheName = NICE_GOODS_PAGE_CACHE;
    	Page<Goods> goodsPage = dao.paginateByCache(cacheName, pageNumber,
    			pageNumber, Const.GOODS_PAGE_SIZE,
    			"select id", "from goods where isNice=true order by isUp desc,createTime desc");
    	return loadModelPage(goodsPage);
    }
    public Page<Goods> getHotPage(int pageNumber){
        String cacheName = HOT_GOODS_PAGE_CACHE;
        Page<Goods> goodsPage = dao.paginateByCache(cacheName, pageNumber,
                pageNumber, Const.GOODS_PAGE_SIZE,
                "select id", "from goods order by isUp desc,pv desc");
        return loadModelPage(goodsPage);
    }
    public Page<Goods> getClosePage(int pageNumber){
    	String cacheName = NICE_GOODS_PAGE_CACHE;
    	Page<Goods> goodsPage = dao.paginateByCache(cacheName, pageNumber,
    			pageNumber, Const.GOODS_PAGE_SIZE,
    			"select id", "from goods where isClose=true order by isUp desc,createTime desc");
    	return loadModelPage(goodsPage);
    }
    public Page<Goods> getNoClosePage(int pageNumber){
        String cacheName = NICE_GOODS_PAGE_CACHE;
        Page<Goods> goodsPage = dao.paginateByCache(cacheName, pageNumber,
                pageNumber, Const.GOODS_PAGE_SIZE,
                "select id", "from goods where isClose=false order by isUp desc,createTime desc");
        return loadModelPage(goodsPage);
    }
    public Page<Goods> getPageForAdmin(Integer pageNumber) {
    	String cacheName = GOODS_PAGE_FOR_ADMIN_CACHE;
    	Page<Goods> goodsPage = dao.paginateByCache(cacheName, pageNumber,
    			pageNumber, Const.PAGE_SIZE_FOR_ADMIN,
    			"select id", "from goods order by isUp desc,createTime desc");
    	return loadModelPage(goodsPage);
    }
    public Page<Goods> getPageByUserID(Integer pageNumber,int userID) {
        String cacheName = GOODS_PAGE_FOR_USER_CACHE;
        Page<Goods> goodsPage = dao.paginateByCache(cacheName, pageNumber,
                pageNumber, Const.POST_PAGE_SIZE,
                "select id", "from goods where userID = ? order by isUp desc,createTime desc",userID);
        return loadModelPage(goodsPage);
    }

    /* other */
    public void increaseGoodsPV(int goodsID){
        Goods goods = dao.findFirst("select pv from goods where id=?", goodsID);
        if (goods != null){
            int pv = goods.getInt("pv");
            new Goods().set("id", goodsID).set("pv", pv + 1).update();
        }
        increaseGoodsAttrInCache(goodsID, "pv");
    }
    public void increaseGoodsPostCount(int goodsID){
        int postCount = dao.findFirst("select postCount from goods where id=?", goodsID).getInt("postCount");
        new Goods().set("id", goodsID).set("postCount", postCount + 1).update();
        increaseGoodsAttrInCache(goodsID, "postCount");
    }
    public void myUpdate(){
        this.filterText("content").update();
        removeThisCache();
        removeAllPageCache();
    }
    public void deleteByID(int goodsID){
        dao.deleteById(goodsID);
        dao.removeAllPageCache();
    }

    /* getter */
    public Admin getUser(){
        return Admin.dao.get(this.getInt("userID"));
    }
    public Module getModule(){
        return Module.dao.get(this.getInt("moduleID"));
    }

    /* cache */
    public void removeThisCache() {
        CacheKit.remove(GOODS_CACHE, this.getInt("id"));
    }
    public void removeAllPageCache() {
    	CacheKit.removeAll(GOODS_PAGE_FOR_USER_CACHE);
        CacheKit.removeAll(GOODS_PAGE_FOR_INDEX_CACHE);
        CacheKit.removeAll(GOODS_PAGE_FOR_MODULE_CACHE);
        CacheKit.removeAll(HOT_GOODS_PAGE_CACHE);
        CacheKit.removeAll(NICE_GOODS_PAGE_CACHE);
        CacheKit.removeAll(GOODS_PAGE_FOR_ADMIN_CACHE);
    }

    /* private */
    private void increaseGoodsAttrInCache(int goodsID, String attr) {
        CacheKit.put(GOODS_CACHE, goodsID, get(goodsID).set(attr, get(goodsID).getInt(attr) + 1));
    }

}